package com.mentalhealthplatform.global.log.manager;

import com.mentalhealthplatform.global.log.dto.ManagerLogDTO;
import com.mentalhealthplatform.global.log.enums.LogStatus;
import com.mentalhealthplatform.global.log.enums.OperationStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;


public interface ManagerLogRepository extends JpaRepository<ManagerLog, Long> {
    void deleteByIdIn(Long[] ids);

    @Query("SELECT new com.mentalhealthplatform.global.log.dto.ManagerLogDTO(" +
            "m.id, u.username, m.systemModule, m.operationType, m.operationAddress, " +
            "m.operationLocation, m.operationStatus, m.logStatus, m.operationTime, m.elapsedTime) " +
            "FROM ManagerLog m " +
            "LEFT JOIN m.operator u " +
            "WHERE (:systemModule IS NULL OR m.systemModule LIKE %:systemModule%) " +
            "AND (:operatorName IS NULL OR u.username LIKE %:operatorName%) " +
            "AND (:logStatus IS NULL OR m.logStatus = :logStatus) " +
            "AND (:operationStatus IS NULL OR m.operationStatus = :operationStatus) " +
            "AND (:startDate IS NULL OR m.operationTime >= :startDate) " +
            "AND (:endDate IS NULL OR m.operationTime <= :endDate) ")
    Page<ManagerLogDTO> findManagerLogsWithFilters(
            @Param("systemModule") String systemModule,
            @Param("operatorName") String operatorName,
            @Param("logStatus") LogStatus logStatus,
            @Param("operationStatus") OperationStatus operationStatus,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate,
            Pageable pageable);


}
